package han.chensing.latos.game.processor;

import java.util.Locale;

import han.chensing.latos.game.elements.character.CharacterInstance;
import han.chensing.latos.game.elements.notes.Note;

public class ComboCounter {
    private int allNotes;
    private int combo;
    private int maxCombo;
    private double score;
    private double judgeScore;
    private double comboScore;
    private boolean isAP;
    private boolean isFC;
    private final CharacterInstance characterInstance;
    private String tempScoreString;
    private String tempComboString;

    public ComboCounter(CharacterInstance characterInstance){
        combo=0;
        maxCombo=0;
        score=0;
        judgeScore=0;
        comboScore=0;
        isAP=true;
        isFC=true;
        this.characterInstance=characterInstance;
        updateTempScore();
    }

    public void updateScore(Note n){
        try {
            Note.HitStatue hitStatue = n.getHitStatue();
            switch (hitStatue) {
                case None:
                    break;
                case Perfect:
                    characterInstance.invokeMessage("onPerfect");
                    update(1, false);
                    break;
                case Good:
                    characterInstance.invokeMessage("onGood");
                    update(0.6d, false);
                    isAP=false;
                    break;
                case Bad:
                    characterInstance.invokeMessage("onBad");
                    update(0, true);
                    isAP=isFC=false;
                    break;
                case Miss:
                    characterInstance.invokeMessage("onMiss");
                    update(0, true);
                    isAP=isFC=false;
            }
        }catch (Exception e){
            e.printStackTrace();
        }
    }

    private void update(double rate,boolean missed){
        double s=rate/(double)allNotes*900000d;
        judgeScore+=s;
        if(!missed){
            combo++;
            maxCombo=Math.max(maxCombo,combo);
        }else{
            combo=0;
        }
        comboScore=((double)maxCombo/(double)allNotes)*100000d;
        score=Math.min(1000000,Math.ceil(judgeScore+comboScore));
        updateTempScore();
    }

    public void updateTempScore(){
        tempScoreString =String.format(Locale.CHINA,"%07d",(int)score);
        tempComboString=String.valueOf(combo);
    }

    public String getScore(){
        return tempScoreString;
    }

    public int getCombo() {
        return combo;
    }

    public String getComboString(){
        return tempComboString;
    }

    public int getAllNotes() {
        return allNotes;
    }

    public void setAllNotes(int allNotes) {
        this.allNotes = allNotes;
    }

    public boolean isAP() {
        return isAP;
    }

    public boolean isFC() {
        return isFC;
    }
}

